package org.jeecg.modules.quartz.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.util.dingtalk.DingMessageUtils;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.message.service.ISysMessageTemplateService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.api.client.util.Maps;

import io.finer.erp.jeecg.bas.entity.ZrkShoporder;
import io.finer.erp.jeecg.bas.service.IZrkPushSetService;
import io.finer.erp.jeecg.bas.service.IZrkShoporderService;
import lombok.extern.slf4j.Slf4j;

/** 
 * @description 销售订单长时间未生成任务单进行提醒
* @author 作者 : xzs
* @version 创建时间：2021-5-27 13:45:39 
*/
@Slf4j
public class ZrkShoporderMessageJob implements Job {

	@Autowired
	private IZrkShoporderService zrkShoporderService;
	@Autowired
	private ISysMessageTemplateService msgTemService;
	@Autowired
	private IZrkPushSetService pushSetService;
	@Autowired
	private ISysUserService sysUserService;
	
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		LambdaQueryWrapper<ZrkShoporder> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ZrkShoporder::getIsPush, 0);
		queryWrapper.eq(ZrkShoporder::getDelFlag, 0);
		List<ZrkShoporder> shoporderList = zrkShoporderService.list(queryWrapper);
		Set<String> username = new HashSet<String>();
		Map<String,StringBuffer> map = Maps.newHashMap();
		//取出消息模板
		if(shoporderList!=null && shoporderList.size()>0) {
			for(ZrkShoporder zrkShoporder:shoporderList) {
				long times = DateUtils.getMillis(new Date())-DateUtils.getMillis(zrkShoporder.getOrderTime());
				if(times > 60*60*1000) {
					if(oConvertUtils.isEmpty(map.get(zrkShoporder.getCreateBy()))) {
						StringBuffer buffer = new StringBuffer();
						buffer.append(zrkShoporder.getOrderNo());
						buffer.append(",");
						username.add(zrkShoporder.getCreateBy());
						map.put(zrkShoporder.getCreateBy(), buffer);
					}else {
						map.put(zrkShoporder.getCreateBy(), 
						map.get(zrkShoporder.getCreateBy())
						.append(zrkShoporder.getOrderNo())
						.append(","));
					}
				}
			}
			SysMessageTemplate template = msgTemService.lambdaQuery().eq(SysMessageTemplate::getTemplateCode, "shoporder_message").one();
			if(template!=null) {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				//查询提醒人钉钉ID
				LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
				query.eq(SysUser::getDelFlag, "0");
				query.in(SysUser::getUsername, username);
				List<SysUser> userList = sysUserService.list(query);
				if(userList!=null&&userList.size()>0) {
					for(SysUser sysUser:userList) {
						if(null == sysUser) {
							log.info("未找到该用户");
							continue;
						}
						if(StringUtils.isBlank(sysUser.getThirdId())) {
							log.info("该用户未分配第三方id");
							continue;
						}
						String message = template.getTemplateContent();
						message = message.replace("${orderNo}", map.get(sysUser.getUsername()).toString());
						message = message.replace("${orderTime}", formatter.format(new Date()));
						DingMessageUtils.sendDingMessage(sysUser.getThirdId(), message);
					}
				}
			}else {
				log.info("没找到消息模板");
			}
			
		}

	}

}
